/**
 * 全局的一些数据
 */

import api from '../../../api'
import types from '../../types'
import {push} from 'react-router-redux';

export function signinSuccess(data) {
    return {
        type: `${types.SIGN_IN}_SUCCESS`,
        payload: {
            msg: data
        }
    }
}

export function signinFail(data) {
    return {
        type: `${types.SIGN_FAIL}_SUCCESS`,
        payload: {
            failMsg: data
        }
    }
}

export function signin(userName, password) {
    return function (dispatch) {
        return api.post('/proxy/wemall/login/check',
            {
                params: {},
                data: {
                    'userName': userName,
                    'password': password
                }
            })
            .then(response => {
                if (response.RET_CODE === "success") {
                    dispatch(signinSuccess(response));
                    localStorage.setItem("userName", userName);
                    dispatch(push('/'));
                } else {
                    dispatch(signinFail(response.RET_MSG));
                }
            }, error=> {
                alert("网络错误" + error);
            })
    }
}
